<?php
/**************************************************************************
	ESAM (EVE to SMF API Module)
	Copyright (C) 2009  Clinton Weiss

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

function template_main() {
	global $context, $txt, $scripturl;

	$esamGlobal = new EsamGlobal();

	echo '<table cellspacing="0" cellpadding="5" border="0" width="100%" class="tborder">
			<tbody><tr class="titlebg">
				<td>'.$txt['eve_general_settings'].'</td>
			</tr><tr>
				<td class="windowbg2">';
	if ( isset($context['extra_text']) ) {
		echo '<p><strong>'.$context['extra_text'].'</strong></p>';
	}
	echo '<form action="?action=EsamSettings;sa=index" method="post" accept-charset="', $context['character_set'], '">';

	// Import API and rules from older versions of ESAM
	if ( isset($_GET['legacy']) && $_GET['legacy'])
	{
		// let's make sure the legacy tables exist
		$esamQuery = new EsamQuery();
		$esamQuery->setQuery("select * from {db_prefix}eve_api_keys");
		$legacyApiKeys = $esamQuery->getResultSet();

		$esamQuery->setQuery("select * from {db_prefix}eve_rules");
		$legacyRules = $esamQuery->getResultSet();

		echo '<fieldset><legend>Legacy</legend>';
		echo '<input type="checkbox" name="import_legacy" id="import_legacy"/> ';
		echo '<label for="need_to_enter"> Import Legacy API and Rules</label><br />';

		echo '<input type="checkbox" name="remove_legacy" id="import_legacy"/> ';
		echo '<label for="need_to_enter"> Remove Legacy ESAM tables</label><br />';
		echo '</fieldset>';
	}

	echo '<input type="checkbox" name="api_required" id="api_required"';
	if ( $esamGlobal->getGlobalSetting('GLOBAL_REQUIRED_API', "false") == "true") {
		echo 'checked="checked"';
	}
	echo ' /> ';
	echo '<label for="need_to_enter">'.$txt['eve_must_use_api'].'</label><br />';

	echo '<input type="checkbox" name="display_eve_name" id="display_eve_name"';
	if ( $esamGlobal->getGlobalSetting('DISPLAY_EVE_NAME', "false") == "true") {
		echo 'checked="checked"';
	}
	echo ' /> ';
	echo '<label for="need_to_enter">'.$txt['display_eve_name'].'</label><br />';

	echo '<input type="checkbox" name="add_corp_ticker" id="add_corp_ticker"';
	if ( $esamGlobal->getGlobalSetting('add_corp_ticker', "false") == "true") {
		echo 'checked="checked"';
	}
	echo ' /> ';
	echo '<label for="need_to_enter">'.$txt['add_corp_ticker'].'</label><br />';


	echo '<br /><input type="submit" name="submit" value="'.$txt['eve_submit'].'"/></form>';
	echo '</td></tr></tbody></table>';
	echo '<br><a href="' . $scripturl . '?action=EsamSettings;sa=index&legacy=1">Legacy Support</a>';
}

function template_rules() {
	global $context, $txt, $scripturl, $modSettings;

	echo '<form action="?action=EsamSettings;sa=rules" method="post" accept-charset="', $context['character_set'], '">';
	echo '<table cellspacing="0" cellpadding="5" border="0" width="17em" class="tborder">
		<tbody><tr class="titlebg">
			<td colspan="2">'.$txt['eve_create_rule'].'</td></tr>';
	echo '<tr><td>'.$txt['eve_create_rules1'].'</td><td>';
	echo '<input type="radio" name="roleType" group="roleType" value="member" checked="true">'.$txt['eve_create_rules2'].'</input>';
	echo '<input type="radio" name="roleType" group="roleType" value="roleDirector">'.$txt['eve_create_rules3'].'</input></td></tr>';
	echo '<tr><td>'.$txt['eve_create_rules4'].'</td><td><input type="text" name="targetName" /></td></tr>';

  echo '<tr><td><input type="checkbox" name="includeSkill" value="skill">Has the skill: </input></td>';
  echo '<td><select name="skillTypeId" id="skillTypeId">';
  $skillArray = getSkillArray();
  foreach (array_keys($skillArray) as $skillTypeID)
  {
    echo '<option value=' . $skillTypeID . '>' . $skillArray[$skillTypeID] . '</option>';
  }
  echo '</select></td></tr>';

  echo '<tr><td>At level:</td><td>';
  for ($a=1; $a<=5; $a++) echo "<input type='radio' name='skillLvl' group='skillLvl' value='$a' " . ($a == 1 ? "checked='true'" : "") ."> $a ";
  echo '</td></tr>';


	echo '<tr><td>'.$txt['eve_create_rules5'].'</td>';

  // SMF has a braindead policy about ()'s within queries
	// temporarily disabling this policy
	$modSettings['disableQueryCheck'] = 1;
	$esamQuery = new EsamQuery();
	$esamQuery->setQuery($esamQuery->Q_MGROUP_LIST);
	$memberGroups = $esamQuery->getResultSet();
	$modSettings['disableQueryCheck'] = 0;

	echo '<td><select name="memberGroupId" id="memberGroupId">';
	foreach ($memberGroups as $memberGroup)
	{
		echo '<option value=' . $memberGroup['ID_GROUP'] . '>' . $memberGroup['groupName'] . '</option>';
	}
	echo '</select></td></tr>';

	echo '<tr><td><input type="submit" name="submit" value="' . $txt['eve_submit'] . '"/></td></tr></tbody></table>';
	echo '</form>';

	echo '<table cellspacing="0" cellpadding="5" border="0" width="100%" class="tborder">
		<tbody><tr class="titlebg">
			<td>'.$txt['eve_current_rules'].'</td></tr>';

	// Prepare Alliance information
	$ea = new EsamEveApi();
	$data = $ea->getAllianceList();
	$alliance_xml = new SimpleXMLElement($data);
	$data = null; // Save some space...

	$esamAlliance = new EsamAlliance();
	$esamGlobal = new EsamGlobal();
	$esamRules = new EsamRules();
	$rules = $esamRules->listRules();

	foreach($rules as $rule) {
		$rule_type = $rule['TYPE'];
		$rule_requires_director_role = ($rule['ROLE'] == 'roleDirector');
		$member_status = $rule_requires_director_role ? " director " : "member";
		$rule_id = $rule['ID_RULE'];
		$rule_role_id = $rule['ROLE_ID'];
		$member_group = $rule['GROUP_ID']; // Member Group
		$member_group_name = $esamGlobal->getGroupName($member_group);
		$member_group_link = "<a href='$scripturl?action=membergroups;sa=members;group=$member_group'>$member_group_name</a>";

    $skillTypeId = $rule['SKILL_ID'];
    $skillLvl = $rule['SKILL_LVL'];
    $skillDisplay = "";
    if ($skillTypeId != "")
    {
      $skillsArray = getSkillArray();
      $skillDisplay = " and has the skill " . $skillsArray["$skillTypeId"] . " trained to at least " . $skillLvl;
    }


		switch($rule_type)
		{
			case 'A': // Alliance rules
				echo '<tr><td class="windowbg2">';
				echo 'If user is a ' . $member_status . ' of corp within alliance <b>' . $esamAlliance->getAllianceName($rule_role_id) . '</b>' . $skillDisplay . ' they will be added to MemberGroup ' . $member_group_link . '<br />';

				$alliance_info = $alliance_xml->xpath("//eveapi/result/rowset/row[@allianceID='" . $rule_role_id . "']/rowset");
				$alliance_info_xml = $alliance_info[0];
				$corps = $alliance_info_xml->xpath("./row/@corporationID");
				foreach ($corps as $corp)
				{
					$corporation_id = $corp->corporationID;
					echo "<li style='margin-left: 2em;'>" . $esamAlliance->getCorporationName($corporation_id) . "</li>";
				}
				break;
			case 'P': // Pilot
				echo '<tr><td class="windowbg2">';
				echo 'If user is pilot <b>' . $esamAlliance->getCharacterName($rule_role_id) . '</b>' . $skillDisplay . ' they will be added to MemberGroup ' . $member_group_link;
				break;
			case 'C': // Corporation rules
				echo '<tr><td class="windowbg2">';
				echo 'If user is a ' . $member_status . ' of corp <b>' . $esamAlliance->getCorporationName($rule_role_id) . '</b>' . $skillDisplay . ', they will be added to MemberGroup ' . $member_group_link ;
				break;
		}
		$removeUrl = $scripturl . '?action=EsamSettings;sa=delete_rule;rule_id=' . $rule_id;
		echo "<em style='margin-left: 1em;'><a style='color: red; ' href='$removeUrl'>Remove</a></em>";
		echo '</td></tr>';
	}

	echo '</tbody></table>';
}

function template_validator()
{
	global $txt;

	echo '<table cellspacing="0" cellpadding="5" border="0" width="100%" class="tborder">
		<tbody><tr class="titlebg">
			<td>'.$txt['eve_validation_results'].'</td>
		</tr><tr>
			<td class="windowbg2">';

	$esamValidator = new EsamValidator();
	$esamValidator->setVerbosity(1);
	$esamValidator->validateApiKeys();

	echo '</td></tr></tbody></table>';
}
?>
